package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.icecredit.IceCreditDayPupilData;
import com.ucredit.babel.common.model.icecredit.IceCreditDayPupilRecord;
import com.ucredit.babel.common.model.icecredit.IceCreditEyeData;
import com.ucredit.babel.common.model.icecredit.IceCreditEyeRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @Classname BingJianEyeParse
 * @Description 冰鉴-慧眼大额分 20210423-20210531
 * @Date 2021/6/9 21:05
 * @Created by lzw
 */
public class BingJianEyeParse extends ParseBase {
    Text key = new Text();
    Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        IceCreditEyeRecord obj = (IceCreditEyeRecord) model;
        StringBuilder sb = getBaseInfo(obj, successful);

        IceCreditEyeData info = obj.getData();

//        sb.append(info.getCode()).append(CRT001);
//        sb.append(info.getMessage()).append(CRT001);
//        sb.append(info.getTraceId()).append(CRT001);

        String string = info.getData();

        JSONObject data = JSON.parseObject(string) == null ? new JSONObject() : JSON.parseObject(string);
        JSONObject result = data.getJSONObject("result") == null ? new JSONObject() : data.getJSONObject("result");

        JSONObject basic_large_repayment_score_2020_v2 = result.getJSONObject("basic_large_repayment_score_2020_v2") == null ? new JSONObject() : result.getJSONObject("basic_large_repayment_score_2020_v2");
        sb.append(data.getString("response_code") == null ? "" : data.getString("response_code")).append(CRT001);
        sb.append(data.getString("message") == null ? "" : data.getString("message")).append(CRT001);
        sb.append(data.getString("trace_id") == null ? "" : data.getString("trace_id")).append(CRT001);

        sb.append(basic_large_repayment_score_2020_v2.getString("bj_score") == null ? "" : basic_large_repayment_score_2020_v2.getString("bj_score")).append(CRT001);

        key.set(getKey());
        value.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, value);


        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_bingjian_eye_inc";
    }

    @Override
    public String getType() {
        return RecordType.ICE_CREDIT_EYE_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "冰鉴-慧眼大额分";
    }
}
